﻿Type.registerNamespace("Liberty.Controls.Rules");

//AbstractRule
Liberty.Controls.Rules.AbstractRule = function() {}
Liberty.Controls.Rules.AbstractRule.prototype = function() {
    Execute: function() {
        return false;
    }
}
Liberty.Controls.Rules.AbstractRule.registerClass( 'Liberty.Controls.Rules.AbstractRule', Sys.Component );

//ValidationRule
Liberty.Controls.Rules.ValidationRule = function( validatorID ) {
    Liberty.Controls.Rules.ValidationRule.initializeBase( this );
    this._ValidatorID = validatorID;
}

Liberty.Controls.Rules.ValidationRule.prototype = {
    get_ValidatorID: function(){
        return this._ValidatorID;
    },
    
    set_ValidatorID: function( value ) {
        this._ValidatorID = value;
    },
    
    Validate: function() {
        var result = false;
        var validatorControl = eval( this._ValidatorID );
        if ( typeof( validatorControl.evaluationfunction ) == "function" ) {
            result = validatorControl.evaluationfunction( validatorControl );
        }
        return result;
    },
    
    Execute: function() {
        return this.Validate();
    },
    
    ReSubscribe: function( validationRule ) {
        var validatorControl = eval( this._ValidatorID );
        if( validatorControl && validatorControl.controltovalidate )
        {
            var element = $get( validatorControl.controltovalidate );
            $clearHandlers( element );
            this.Subscribe( validationRule );
        }
    },
    
    Subscribe: function( validationRule ) {
        var validatorControl = eval( this._ValidatorID );
        if( validatorControl && validatorControl.controltovalidate )
        {
            var element = $get( validatorControl.controltovalidate );
            var handler = Function.createDelegate( validationRule, validationRule.Execute )
            this.ControlHookup( element, handler );
        }
    },
    
    ControlHookup: function( element, handler )
    {
        var controlType =  element.type;
        var eventType = "";
        var causeValidationOnKeypress = "false";
        var CLICK_EVENT = "click";
        var KEY_PRESS_EVENT = "keypress";
        var CHANGE_EVENT = "change";
        var PROPERTY_CHANGE_EVENT = "propertychange";
        switch( controlType )
        {
            case "radio" :
                eventType = CLICK_EVENT;
                break;
            case "checkbox" :
                eventType = CLICK_EVENT;
                break;
            case "password":
            case "file":
                eventType = KEY_PRESS_EVENT;
                break;
            case "text":
                causeValidationOnKeypress = element.CauseValidationOnKeyPress;
                if( causeValidationOnKeypress && causeValidationOnKeypress.toLowerCase() == "false" )
                {   
                    eventType = CHANGE_EVENT;
                }
                else
                {
                    eventType = PROPERTY_CHANGE_EVENT;
                }
                break;
            default :
                if( element.tagName == "SELECT" )
                {
                    eventType = "change";
                }
                else
                {
                    eventType = PROPERTY_CHANGE_EVENT;
                }
                break;
        }
        $addHandler( element, eventType, handler );
    }
}

Liberty.Controls.Rules.ValidationRule.registerClass('Liberty.Controls.Rules.ValidationRule', Liberty.Controls.Rules.AbstractRule );

//ValidationSummaryRule
Liberty.Controls.Rules.ValidationSummaryRule = function( validatorID, validationGroup ) {
    Liberty.Controls.Rules.ValidationSummaryRule.initializeBase( this, [validatorID] );
    this._ValidationGroup = validationGroup;
}

Liberty.Controls.Rules.ValidationSummaryRule.prototype = {
    get_ValidationGroup: function(){
        return this._ValidationGroup;
    },
    
    set_ValidationGroup: function( value ) {
        this._ValidationGroup = value;
    },
    
    Validate: function() {
        Page_ClientValidate( this._ValidationGroup );
        return Page_IsValid;
    },
    
    Subscribe: function( validationRule ) {
    }
}

Liberty.Controls.Rules.ValidationSummaryRule.registerClass('Liberty.Controls.Rules.ValidationSummaryRule', Liberty.Controls.Rules.ValidationRule );

//RuleSet
Liberty.Controls.Rules.RuleSet = function() {
    Liberty.Controls.Rules.RuleSet.initializeBase( this );
    this.Rules = [];
    this._AllRulesPassed = true;
}

Liberty.Controls.Rules.RuleSet.prototype = {
    
    get_AllRulesPassed: function() {
        return this._AllRulesPassed;
    },
    
    set_AllRulesPassed: function( value ) {
        this._AllRulesPassed = value;
    },
    
    AddRule: function( rule ) {
        Array.add( this.Rules, rule );
    },
    
    Execute: function() {
        this._AllRulesPassed = true;
        Array.forEach( this.Rules, this.EvaluateRule, this );
    },
    
    Activity: function( allRulesPassed ) {
    },
    
    EvaluateRule: function( rule ) {
        if( this.get_AllRulesPassed() && !rule.Validate() )
            this.set_AllRulesPassed( false );
    }
    
}
Liberty.Controls.Rules.RuleSet.registerClass('Liberty.Controls.Rules.RuleSet', Liberty.Controls.Rules.AbstractRule );

//ValidationRuleSet
Liberty.Controls.Rules.ValidationRuleSet = function( postbackControl ) {
    Liberty.Controls.Rules.ValidationRuleSet.initializeBase( this );
    this.i_PostbackControl = postbackControl;
}

Liberty.Controls.Rules.ValidationRuleSet.prototype = {
    
    get_PostbackControl: function() {
        return this.i_PostbackControl;
    },
    
    set_PostbackControl: function( value ) {
        this.i_PostbackControl = value;
    },
    
    AddRule: function( rule ) {
        rule.Subscribe( this );
        var arguments = new Array( rule );
        Liberty.Controls.Rules.ValidationRuleSet.callBaseMethod( this, 'AddRule', arguments );
    },
    
    ReSubscribeRule: function( rule ){
        rule.ReSubscribe( this );
    },
    
    ReSubscribe: function() {
        Array.forEach( this.Rules, this.ReSubscribeRule, this );
    },
    
    Execute: function() {
        Liberty.Controls.Rules.ValidationRuleSet.callBaseMethod( this, 'Execute' );
        var control = $get( this.i_PostbackControl );
        if( control )
        {
            control.disabled = !this.get_AllRulesPassed();
        }
    }
}
Liberty.Controls.Rules.ValidationRuleSet.registerClass('Liberty.Controls.Rules.ValidationRuleSet', Liberty.Controls.Rules.RuleSet );

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();